package com.ophiux.cs.infrastructure.repositories.consultBase;

import com.ophiux.cs.domain.consultBase.model.entity.Hospital;
import com.ophiux.cs.domain.consultBase.model.valueobj.HospitalQueryObj;
import com.ophiux.cs.domain.consultBase.repositories.IHospitalRepository;
import com.ophiux.cs.infrastructure.compoment.basedata.mybatis.DataComponent;
import com.ophiux.cs.infrastructure.mapper.consultBase.HospitalMapper;

import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class HospitalRepository implements IHospitalRepository {
	@Autowired
	HospitalMapper hospitalMapper;
	@Autowired
	DataComponent dataComponent;

	@Override
	public void createHospital(Hospital hospital) throws OphiuxRepositoryException {
		try {
			Hospital hos=hospitalMapper.findByName(hospital.getHOSPITAL_NAME());
			if(hos==null){//不存在，新增
				hospitalMapper.addHospital(hospital);
			}else{//存在，更新
				hospital.setID(hos.getID());
				hospitalMapper.updateHospital(hospital);
			}
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("HospitalRepository=>createHospital()异常，",e);
			throw new OphiuxRepositoryException("新增医院异常", e);
		}
	}
	@Override
	public void updateHospital(Hospital hospital)  throws OphiuxRepositoryException {
		try {
			hospitalMapper.updateHospital(hospital);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("HospitalRepository=>updateHospital()异常，",e);
			throw new OphiuxRepositoryException("更新医院异常", e);
		}
	}
	@Override
	public void deleteHospital(String hospitalId) throws OphiuxRepositoryException  {
		try {
			hospitalMapper.deleteHospital(hospitalId);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("HospitalRepository=>deleteHospital()异常，",e);
			throw new OphiuxRepositoryException("删除医院异常", e);
		}
	}

	@Override
	public Hospital getHospitalByHosID(String hospitalId) throws OphiuxRepositoryException{
		Hospital hos=null;
		try {
			hos=hospitalMapper.getHospitalByHospitalID(hospitalId);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("HospitalRepository=>getHospitalByHosID()异常，",e);
			throw new OphiuxRepositoryException("根据医院ID查询异常", e);
		}
		return  hos;
	}

	@Override
	public List<Hospital> selectHospital() throws OphiuxRepositoryException {
		List<Hospital> hosList=null;
		try {
			hosList=hospitalMapper.selectHospitalByCondition(null);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("HospitalRepository=>selectHospital()异常，",e);
			throw new OphiuxRepositoryException("查询医院异常", e);
		}
		return  hosList;
	}

	@Override
	public List<Hospital> getHospitalByConditions(HospitalQueryObj hospital) throws OphiuxRepositoryException{
		List<Hospital> hosList=null;
		try {
			hosList=hospitalMapper.selectHospitalByCondition(hospital);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("HospitalRepository=>getHospitalByConditions()异常，",e);
			throw new OphiuxRepositoryException("条件查询医院异常", e);
		}
		return  hosList;
	}
}
